package ar.edu.utn.frsf.cidisi.upcolbpip.verification;

import java.util.List;
import java.util.Vector;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import ar.edu.utn.frsf.cidisi.upcolbpip.UPColBPIPModel;
import ar.edu.utn.frsf.cidisi.upcolbpip.collaborativebusinessprocesses.CollaborativeBusinessProcess;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocol;
import ar.edu.utn.frsf.cidisi.upcolbpip.presentation.UpcolbpipEditorPlugin;

/**
 * @generated
 */
public class UpcolbpipVerifyAntiPatternsAction implements IObjectActionDelegate {

	private static String TARGETDIR = "";

	/**
	 * @generated
	 */
	private IWorkbenchPart targetPart;

	/**
	 * @generated
	 */
	private URI domainModelURI;

	/**
	 * @generated
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.targetPart = targetPart;
	}

	/**
	 * @generated
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		domainModelURI = null;
		action.setEnabled(false);
		if (selection instanceof IStructuredSelection == false
				|| selection.isEmpty()) {
			return;
		}
		IFile file = (IFile) ((IStructuredSelection) selection)
				.getFirstElement();
		domainModelURI = URI.createPlatformResourceURI(file.getFullPath()
				.toString(), true);
		action.setEnabled(true);
	}

	/**
	 * @generated
	 */
	private Shell getShell() {
		return targetPart.getSite().getShell();
	}

	/**
	 * @generated
	 */
	public void run(IAction action) {
		
		
		EObject diagramRoot = null;
		try {
			// Create a resource set
			//
			ResourceSet resourceSet = new ResourceSetImpl();

			Resource resource = resourceSet.getResource(domainModelURI, true);
			diagramRoot = (EObject) resource.getContents().get(0);
		} catch (WrappedException ex) {
			UpcolbpipEditorPlugin.INSTANCE.getPluginLogger().log(
					"Unable to load resource: " + domainModelURI + "\n" + ex); //$NON-NLS-1$
		}
		if (diagramRoot == null) {
//			MessageDialog
//					.openError(
//							getShell(),
//							Messages.UpcolbpipInitDiagramFileAction_InitDiagramFileResourceErrorDialogTitle,
//							Messages.UpcolbpipInitDiagramFileAction_InitDiagramFileResourceErrorDialogMessage);
			return;
		}
		
		if (diagramRoot instanceof UPColBPIPModel){
			
			CollaborativeBusinessProcess cbp = ((UPColBPIPModel)diagramRoot).getB2bCollaboration().getProcesses().get(0);
			
			List<InteractionProtocol> ips = cbp.getInteractionProtocols();
			int size = ips.size();
			
	        System.out.println();
	        System.out.println();
			System.out.println("*****************************************************************");
			System.out.println("*                      Start Verification                       *");
			System.out.println("*****************************************************************");
			System.out.println("PNC Id"+"\t"+"Sound"+"\t"+"Total Time [ms]");
			
			for (int i=0;i<size;i++){
					
				InteractionProtocol ip = ips.get(i);
				
				System.out.print(ip.getName()+"\t");
				
				double time = 0.0;

				Diagnostic diagnostic = null;
				
				for (int iAvg=0;iAvg<100;iAvg++){

					long startTime = System.nanoTime();				

					diagnostic = Diagnostician.INSTANCE.validate(ip);
			
					time = time + new Double((System.nanoTime()-startTime))/1000/1000;
					
				}
				
				if (diagnostic.getSeverity() == Diagnostic.OK)
					System.out.print(true+"\t");
				else
					System.out.print(false+"\t");

				System.out.println(time/100);

				
			}
			
	        System.out.print("\n");
			System.out.println("*****************************************************************");
			System.out.println("*                        End Verification                       *");
			System.out.println("*****************************************************************");

			//			MessageDialog
//			.openInformation(
//					getShell(),
//					"UP-ColBPIP",
//					"Petri Net specification was generated on " + ip.getName());
		}
		
	}
	
	
}
